define(["jquery"], function($) {
	function RegisterModal() {
		this.render();
		this.addLisenter();
	}

	RegisterModal.registerModalTemplate = `<div class="modal fade" id="registerModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	        <h4 class="modal-title" id="registerModalLabel">用户注册</h4>
	      </div>
	      <div class="modal-body">
	     <form class="form-a">
			  <div class="form-group">
			    <label for="registerUsername">用户名</label>
			    <input type="text" name="username" class="form-control" id="registerUsername" placeholder="由英文字母和数字组成的4-16位字符，以字母开头">
			  </div>
			  <div class="form-group">
			    <label for="registerPassword">密码</label>
			    <input type="passwordd" name="password" class="form-control" id="registerPassword" placeholder="由英文字母和数字组成的4-10位字符">
			  </div>
			  <div class="form-group">
			    <label for="registerEmail">邮箱</label>
			    <input type="email" name="email" class="form-control" id="registerEmail" placeholder="邮箱账号@域名。如good@tom.com、whj@sina.com.cn">

			  </div>
			</form>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
	        <button type="button" class="btn btn-primary" id="zhuce">注册</button>
	      </div>
	    </div>
	  </div>
	</div>`;

	$.extend(RegisterModal.prototype, {
		render(){
			$(RegisterModal.registerModalTemplate).appendTo("body");
		},
		 registerHander(){
			// 获取登录表单中用户名与密码（待传递到服务器的数据）
			const data = $(".form-a").serialize();
			console.log(data);
			// API接口
			$.post("/api/users/register.do", data, (data)=>{
				if (data.res_body.ret.code === 0) { // 用户名或密码错误
					// $(".login-error").removeClass("hidden");
					// console.log("失败");

				} else { // 登录成功
					// 保存登录成功的用户信息
					// sessionStorage.loginUser = data.res_body.ret.data.username;
					// // 刷新页面
					// location.reload();
					// console.log("成功")
				}
			}, "json")
		 },
		 addLisenter(){
				$("#registerUsername").blur(()=>{
					// console.log("blur");
					this.testUsername();
				});
				$("#registerPassword").blur(()=>{
				
					this.Passwords();
				});
				$("#registerEmail").blur(()=>{
					this.Emails();

				});
				$("#zhuce").click(()=>{
					console.log(this);
					this.registerHander();
				})


		 },
		 //验证用户名
		 testUsername(){			 
				var userName = $("#registerUsername").val();
				var regName = /[a-zA-Z]\w{4,16}/
				if (userName == "" || userName.trim() == "") {
					console.log("请输入用户名");
					return false;
				} else if (!regName.test(userName)) {
					console.log("由英文字母和数字组成的4-16位字符，以字母开头");
					return false;
				} else {
					console.log("ok!");
					return true;
				}
			},

  	//验证密码
			Passwords(){
				var pwd = $("#registerPassword").val();
				var regPwd = /^\w{4,10}$/;
				if (pwd == "" || pwd.trim() == "") {
					console.log("请输入密码");
					return false;
				} else if (!regPwd.test(pwd)) {
				console.log("格式错误");
					return false;
				} else {
				  console.log("ok!!!");
					return true;
				}
			},
				//邮箱验证
				Emails(){
					var email = $("#registerEmail").val();
					var regEmail = /^\w+@\w+((\.\w+)+)$/;
					if(email == "" || email.trim() == ""){
						console.log("请输入邮箱");
						return false;
					} else if (!regEmail.test(email)) {
					console.log("输入的邮箱格式不正确");
						return false;
					} else {
					console.log("ok!!!");
						return true;
					}
				
				},
				

	});

	 return RegisterModal;
});